﻿using UnityEngine;
using UnityEngine.Profiling;
using WorldMapBase.Display;
using WorldMapBase.Tile.Pool;
using Yoozoo.Libs;

namespace WorldMapBase.Tile
{
    /// <summary>
    /// 物件实例化对像
    /// </summary>
    public class WorldObject : IClass, IDisplayElement
    {
        public bool isCreating
        {
            get
            {
                return m_isCreate;
            }
        }

        public System.Action<WorldObject> onCreateFinish;

        protected TreeObject c;

        /// <summary>
        /// 目标对像
        /// </summary>
        private Transform m_RefTrans;

        private Vector3 m_position;

        private Vector3 m_scale;

        private Vector3 m_rotation;

        private int m_resID = -1;

        private bool m_isCreate;

        public int ResID
        {
            get { return m_resID; }
        }

        public void OnReset()
        {
            if (null != m_RefTrans)
            {
                m_RefTrans = null;
            }

            collider = null;
        }

        public void SetResID(int resId)
        {
            m_resID = resId;
        }

        public int GetResID()
        {
            return m_resID;
        }

        public void OnDestroy()
        {
        }

        public virtual void SetPosition(Vector3 position)
        {
            m_position = position;
            if (m_RefTrans != null)
            {
                m_RefTrans.position = m_position;
            }
        }

        public void SetRotation(Vector3 rot)
        {
            m_rotation = rot;
            if (m_RefTrans != null)
            {
                m_RefTrans.localEulerAngles = m_rotation;
            }
        }

        public void SetScale(Vector3 scl)
        {
            m_scale = scl;
            if (m_RefTrans != null)
            {
                /*if (m_RefTrans.childCount > 0)
                {
                    //根结点要设置一下，否则就是（0，0，0）了
                    m_RefTrans.transform.localScale = Vector3.one;
                    for (int i = 0; i < m_RefTrans.childCount; i++)
                    {
                        m_RefTrans.GetChild(i).localScale = scl;
                    }
                }
                else*/
                {
                    m_RefTrans.localScale = scl;
                }
            }
        }

        public virtual void SetOpenUp(int lodLv, float percent)
        {

        }

        public virtual void OnCreate(int lodIndex,int resID,Transform tf)
        {
            m_resID = resID;
            m_isCreate = true;
            m_RefTrans = tf;
            ResetTransform();
            FindCollider();
            //WorldGObjPool.SetChild(m_RefTrans);
            onCreateFinish?.Invoke(this);
            //ShowCollider(WorldEntry.bTerrainColliderShow);
        }

        public virtual void OnRecovered()
        {
            ClassPoolManager.RecoverWorldObject(this);
        }

        public Transform GetRefTrans
        {
            get
            {
                return m_RefTrans;
            }
        }

        private void ResetTransform()
        {
            m_RefTrans.position = m_position;
            m_RefTrans.localScale = m_scale;
            m_RefTrans.eulerAngles = m_rotation;
        }

        public void OnCreate(int resID)
        {
        }

        private GameObject collider;

        private const string colliderNamePostfix = "_col";
        
        private void FindCollider()
        {
            //现在迁城不用collider了,由于会导致gc,所以先删了
            /*for (int i = 0;i < m_RefTrans.childCount; i++)
            {
                Transform t = m_RefTrans.GetChild(i);
                if (t.name.EndsWith(colliderNamePostfix))
                {
                    collider = t.gameObject;
                    break;
                }
            }*/
        }

        // 显示迁城碰撞(舍弃)
        public virtual void ShowCollider(bool enabled)
        {
            if (collider != null)
            {
                collider.SetActive(enabled);
            }
        }
    }
}
